package com.example.physicalexaminationsystem.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.example.physicalexaminationsystem.dto.ResponseDto;
import com.example.physicalexaminationsystem.mapper.ExaminerDao;
import com.example.physicalexaminationsystem.mapper.MealDao;
import com.example.physicalexaminationsystem.model.ExaminerEntity;
import com.example.physicalexaminationsystem.model.MealEntity;
import com.example.physicalexaminationsystem.vo.ExaminerVO;
import com.example.physicalexaminationsystem.vo.MealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.physicalexaminationsystem.constans.Constants.PAY_STATUS_PAID;
import static com.example.physicalexaminationsystem.constans.Constants.PAY_STATUS_UNPAID;

/**
 * @author shiwenzhuo
 * 
 * @description
 */
@Component
public class RegistrationService {

    @Resource
    private ExaminerDao examinerDao;
    @Autowired
    private MealDao mealDao;
    @Autowired
    private MealService mealService;

    //获取体检费用信息
    public Integer getExaminationFee(String name) {
        ExaminerEntity param = new ExaminerEntity();
        param.setName(name);
        ExaminerEntity examinerEntity = examinerDao.selectOne(param);
        Long mealId = examinerEntity.getMealId();
        MealEntity mealEntity = mealDao.selectById(mealId);
        return mealEntity.getPrice();
    }

    //体检付费
    public boolean pay(String name) {
        ExaminerEntity param = new ExaminerEntity();
        param.setName(name);
        ExaminerEntity examinerEntity = examinerDao.selectOne(param);
        Long mealId = examinerEntity.getMealId();
        MealEntity mealEntity = mealDao.selectById(mealId);
        if (mealEntity.getPrice() == 0) {
            return false;
        }
        examinerEntity.setPayStatus(PAY_STATUS_PAID);
        examinerDao.updateById(examinerEntity);
        return true;
    }

    public ResponseDto appointment(Map<String, Object> params) {
        String name = params.get("name").toString();
        String gender = params.get("gender").toString();
        Long mealId = Long.valueOf(params.get("mealId").toString());
        MealEntity mealEntity = mealDao.selectById(mealId);
        if (mealEntity == null) {
            return ResponseDto.fail("套餐不存在");
        }
        DateTime birthday = DateUtil.parse(params.get("birthday").toString());
        DateTime appointmentTime = DateUtil.parse(params.get("appointmentTime").toString());
        ExaminerEntity examinerEntity = new ExaminerEntity();
        examinerEntity.setName(name);
        examinerEntity.setGender(gender);
        examinerEntity.setMealId(mealId);
        examinerEntity.setBirthday(birthday);
        examinerEntity.setPayStatus(PAY_STATUS_UNPAID);
        examinerEntity.setAppointmentTime(appointmentTime);
        examinerDao.insert(examinerEntity);
        return ResponseDto.success();
    }

    public void cancelAppointment(String name) {
        Map<String, Object> param = new HashMap<>();
        param.put("name", name);
        examinerDao.deleteByMap(param);
    }

    public ExaminerVO getAppointmentInfo(String name) {
        ExaminerEntity param = new ExaminerEntity();
        param.setName(name);
        ExaminerEntity examinerEntity = examinerDao.selectOne(param);
        return getExaminerVOByExaminerEntity(examinerEntity);
    }

    public ExaminerVO getExaminerVOByExaminerEntity(ExaminerEntity examinerEntity) {
        ExaminerVO examinerVO = new ExaminerVO();
        Long mealId = examinerEntity.getMealId();
        MealEntity mealEntity = mealDao.selectById(mealId);
        MealVO mealVO = mealService.getMealVOByMealEntity(mealEntity);
        examinerVO.setName(examinerEntity.getName());
        examinerVO.setAppointmentTime(examinerEntity.getAppointmentTime());
        examinerVO.setBirthday(examinerEntity.getBirthday());
        examinerVO.setGender(examinerEntity.getGender());
        examinerVO.setMealId(examinerEntity.getMealId());
        examinerVO.setPayStatus(examinerEntity.getPayStatus() == 1 ? "已支付" : "未支付");
        examinerVO.setMeal(mealVO);
        return examinerVO;
    }

    public List<ExaminerVO> getAppointmentInfo() {
        List<ExaminerVO> result = new ArrayList<>();
        List<ExaminerEntity> examinerEntities = examinerDao.selectList(null);
        for (ExaminerEntity examinerEntity : examinerEntities) {
            result.add(getExaminerVOByExaminerEntity(examinerEntity));
        }
        return result;
    }
}
